<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../../../icon/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="../../../icon/font-awesome/css/font-awesome.min.css" />
    <style type="text/css">
        bady{
          background: #000;
        }
        header {
            padding: 1.6rem 0.8rem 0.5rem 0.8rem;
            width: 100%;
            /*border-bottom: 1px solid #ccc*/
        }
        header span:nth-of-type(1) {
            font-size: 1rem
        }
        header span:nth-of-type(2) {
            font-size: 1rem
        }
       span,.icon-fanhui{
          color:#FFF;
        }

        .btn{
          position: absolute;
          bottom: 1rem;
          left: 0;
          right: 0;
          z-index: 999;
          padding: 0 1rem;
        }
        .btn-bottom{
          font-size: 1rem;
        }
    </style>
</head>

<body id="bd" style="background-color:#000;">
    <div class="main">
      <header>
          <span class="iconfont icon-fanhui" onClick="api.closeWin()"></span>
          <span>头像裁剪</span>
      </header>

        <div class="btn">
            <span id="openClip" style="display:none;" tapmode="hover" onclick="fnOpen()">open</span>
            <span class="btn-bottom" tapmode="hover" onclick="fnSave()">保存</span>
            <span style="float:right;" class="btn-bottom" tapmode="hover" onclick="reset1()">重置</span>
            <!-- <span tapmode="hover" onclick="getPicture()">getPicture</span> -->
        </div>
    </div>
</body>
<script type="text/javascript" src="../../../script/api.js"></script>
<script type="text/javascript" src="../../../lib/zepto.js"></script>
<script type="text/javascript" src="../../../script/dns.js"></script>
<script type="text/javascript" src="../../../script/aui-toast.js"></script>
<script type="text/javascript">
    apiready = function() {
        fnOpen();
    };
      var toast = new auiToast({})
    function fnOpen() {
        var FNImageClip = api.require('FNImageClip');
        FNImageClip.open({
            rect: {
                x: 0,
                y: api.winHeight / 5,
                w: api.winWidth,
                h: 300
            },
            srcPath: api.pageParam.imgUrl,
            highDefinition: true,
            style: {
                mask: 'rgba(0,0,0,0)',
                clip: {
                    w: 150,
                    h: 150,
                    x: api.winWidth / 2 - 80 ,
                    y: 80,
                    borderColor: '#0f0',
                    borderWidth: 5,
                    //appearance: 'circular',
                    appearance: 'rectangle'
                }
            },
            mode: "all",
            fixedOn: api.frameName,
            fixed: false
        }, function(ret, err) {
            if (ret) {
                console.log(JSON.stringify(ret));
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function fnSave() {
        var FNImageClip = api.require('FNImageClip');
        var timestamp = new Date().getTime();
        FNImageClip.save({
            destPath: 'fs://image/' + timestamp + '.jpg',
            copyToAlbum: true,
            quality: 1
        }, function(ret, err) {
            if (ret) {
                console.log(JSON.stringify(ret));
                var Suffixname = JSON.stringify(ret.destPath);
                var index = Suffixname.lastIndexOf(".");
                Suffixname = Suffixname.substring(index + 1, Suffixname.length - 1); //获取后缀名 拼接 到 base64传给服务端
                var trans = api.require('trans'); //图片转Base64
                trans.decodeImgToBase64({
                    imgPath: ret.destPath
                }, function(ret, err) {
                    if (ret.status) {
                        var base64Str = 'data:image/jpg;base64,' + JSON.stringify(ret.base64Str).replace(/\"/g, "");
                        uploadImage(base64Str);
                    } else {
                        toast.fail({
                            title: "网络错误，稍后再试",
                            duration: 1500
                        });
                    }
                });
                    fnClose();
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function fnClose() {
        var FNImageClip = api.require('FNImageClip');
        FNImageClip.close();
    }

    function reset1() {
        var FNImageClip = api.require('FNImageClip');
        FNImageClip.reset();
    }

    //上传图片
    function uploadImage(str) {
        $.ajax({
            type: "post",
            url: dns + 'upload/image/base64',
            data: {
                'base64': str
            },
            dataType: 'json',
            beforeSend: function(request) {
                request.setRequestHeader("token", localStorage.getItem('token'));
            },
            success: function(data) {
                  var imgId = data.data.id;
                //更新头像
                $.ajax({
                    url: dns + 'user/update/user',
                    type: 'POST',
                    data: {
                        'avatar': imgId
                    },
                    beforeSend: function(request) {
                        request.setRequestHeader("token", localStorage.getItem('token'));
                    },
                    success: function(ret) {
                        console.log(JSON.stringify(ret));
                        toast.success({
                            title: "修改成功!",
                            duration: 1500
                        });
                        // 监听更新头像之后刷新个人中心/ 个人信息 的头像
                        api.sendEvent({
                            name: "header_reload"
                        });
                        api.sendEvent({
                            name: "info_reload"
                        });
                        api.closeWin();
                        // if (api.systemType == "ios") {
                        //     api.closeWin();
                        // }
                    },
                    error: function(msg) {
                        toast.fail({
                            title: "网络错误",
                            duration: 1500
                        });
                    }
                });
            },
            error: function(err) {
                console.log(err)
            }
        });
    }
</script>

</html>
